package scripts;

import javax.swing.JOptionPane;
import bot.ScriptMethods;

public class MarketThieveAndBank extends ScriptMethods implements Runnable {

	private int foodID;
	private int npcID;

	public void start() {
		System.gc();
		getAccountSettings();

		Object[] options2 = {"Guards", "Paladins", "Heroes"};
		String res2 = (String)JOptionPane.showInputDialog(null, "", "Select NPC to thieve", JOptionPane.PLAIN_MESSAGE, null, options2, options2[1]);


		Object[] options = {"Lobsters", "Swordfish", "Sharks"};
		String res1 = (String)JOptionPane.showInputDialog(null, "", "Select food to use", JOptionPane.PLAIN_MESSAGE, null, options, options[2]);

		if (res1.equals("Lobsters")) {
			foodID = 373;
		}

		if (res1.equals("Swordfish")) {
			foodID = 370;
		}

		if (res1.equals("Sharks")) {
			foodID = 546;
		}

		if (res2.equals("Guards")) {
			npcID = 321;
		}

		if (res2.equals("Paladins")) {
			npcID = 323;
		}

		if (res2.equals("Heroes")) {
			npcID = 324;
		}

		debug("Thieving " + res2 + " in Ardounge Market, eating " + res1 + ".");

		scriptRunning = true;
		scriptThread = new Thread(this);
		scriptThread.start();
	}

	public void stop() {
		scriptThread = null;
		scriptRunning = false;
		System.gc();
		debug("Stopped script.");
	}

	public void run() {
		while (scriptRunning) {
			try {
				handleEvents();
				if (bankOpen()) {
					if (invContains(619)) {
						depositAll(619);
						wait(random(600, 800));
					}
					if (invContains(38)) {
						depositAll(38);
						wait(random(600, 800));
					}
					if (invContains(152)) {
						depositAll(152);
						wait(random(600, 800));
					}
					if (invContains(142)) {
						depositAll(142);
						wait(random(600, 800));
					}
					if (invContains(161)) {
						depositAll(161);
						wait(random(600, 800));
					}
					if (invContains(612)) {
						depositAll(612);
						wait(random(600, 800));
					}
					if (invContains(10)) {
						depositAll(10);
						wait(random(600, 800));
					}
					if (invContains(41)) {
						depositAll(41);
						wait(random(600, 800));
					}
					withdraw(foodID, 15);
					wait(600);
					closeBank();
					wait(600);
				}
				if (inCombat()) {
					walkTo(ourPlayerX(), ourPlayerY(), false);
					wait(600);
				}
				if (!inCombat() && getCurrentLevel()[3] <= 30) {
					useItem(foodID);
					wait(600);
				}
				if (ourPlayerY() >= 609) {
					if (invContains(foodID)) {
						walkTo(548, 594, false);
						wait(2000);
					} else {
						if (!bankOpen()) {
							openBank();
							wait(2000);
						}
					}
				} else {
					if (invContains(foodID)) {
						if (!isSleeping() && !inCombat() && getCurrentLevel()[3] > 30) {
							thieveNpc(new int[] { npcID });
							wait(random(600, 800));
						}
					} else {
						walkTo(551, 612, false);
						wait(2000);
					}
				}
				wait(random(50, 80));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void handleEvents() throws Exception {
		if (welcomeBoxOpen())
			setWelcomeBox();
		if (!isLoggedIn())
			doLogin();
		if (getFatigue() > 80 && !isSleeping()) {
			wait(600);
			useItem(1263);
			wait(600);
		}
		if (getFatigueSleeping() < 10 && isSleeping())
			doSleeping();
		if (getFightMode() != fightMode)
			setFightMode();
	}
}